#coding=utf-8
#!python.exe
import os
import ctypes
from PIL import ImageGrab

ieHWND = ctypes.windll.user32.FindWindowA("IEFrame",None)    #获取IE的句柄  ok

class RECT(ctypes.Structure):
    _fields_ = [('left', ctypes.c_long),
                ('top', ctypes.c_long),
                ('right', ctypes.c_long),
                ('bottom', ctypes.c_long)]
    def __str__(self):
        return str((self.left, self.top, self.right, self.bottom))
def capture(hwnd,tempdir):
    """
    对窗口截图,传入窗口句柄及保存位置,生成的文件为temp.jpg
    """
    rect = RECT()
    ctypes.windll.user32.GetWindowRect(ieHWND,ctypes.byref(rect))
    rangle = (rect.left+2,rect.top+2,rect.right-2,rect.bottom-2)
    im = ImageGrab.grab(rangle)
    jpgname = "c:\\temp.jpg"
    Xini1 = os.path.join(tempdir,jpgname)
    Xini111 = Xini1.replace("[url=file://\\]\\","/[/url]")
    im.save(Xini111)  
    return
def test1():
    getdir = os.getcwd()
    HWND_TOP = 0
    SWP_NOMOVE= 2
    SWP_NOSIZE= 1
    SWP_SHOWWINDOW= 64
    ctypes.windll.user32.SetWindowPos(ieHWND, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW)
    import time
    time.sleep(5)#延迟5秒
    capture(ieHWND,getdir)
    return
    
if __name__ == "__main__":
    print "以IE窗口为测试窗口请先开一个IE然后再运行"
    test1()